package com.report.reportserver.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ ClassName: ExportExcelUtil
 * @ Description：导出Excel的工具类
 * @ Modified By：
 * @ Version:
 * @ Author     ：LiYing
 * @ Date       ：Created in 13:12 2022/4/21
 */

public class ExportExcelUtil {
    /*写入文件的上级目录*/
    private static String fileWritePath = "";

    /*生成Excel表格*/
    public static void exportDataExcel(ArrayList<String> titleKeyList, List<Map<String, Object>> srcList, String tableName, HttpServletResponse response) throws IOException {

//        Date date = new Date();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
        String xlsFileName = tableName + ".xlsx";

        Workbook wb = new SXSSFWorkbook(100);   //创建Excel文件
        Sheet sheet = null;     //工作表对象
        Row nRow = null;        //行对象
        Cell nCell = null;      //列对象

        int rowNo = 0;      //总行号
        int pageRowNo = 0;  //页行号

        for (int k = 0; k < srcList.size(); k++) {
            Map<String, Object> srcMap = srcList.get(k);
            //写入300000条后切换到下个工作表
            if (rowNo % 100000 == 0) {
                wb.createSheet("工作簿" + (rowNo / 100000));//创建新的sheet对象
                sheet = wb.getSheetAt(rowNo / 100000);        //动态指定当前的工作表
                pageRowNo = 0;      //新建了工作表,重置工作表的行号为0

                nRow = sheet.createRow(pageRowNo++);  // 定义表头
                // 列数 titleKeyList.size()
                for (int i = 0; i < titleKeyList.size(); i++) {
                    Cell cell_tem = nRow.createCell(i);
                    cell_tem.setCellValue(titleKeyList.get(i));
                }
                rowNo++;

            }
            rowNo++;
            nRow = sheet.createRow(pageRowNo++);    //新建行对象

            // 行，获取cell值
            for (int j = 0; j < titleKeyList.size(); j++) {
                nCell = nRow.createCell(j);
                if (srcMap.get(titleKeyList.get(j)) != null) {
                    nCell.setCellValue(srcMap.get(titleKeyList.get(j)).toString());
                } else {
                    nCell.setCellValue("");
                }
            }
        }

        String path = "D:\\Downloads\\" + xlsFileName;
        System.out.println(path);
        File writeFile = new File(path);
        boolean OK = writeFile.createNewFile();
        FileOutputStream out = new FileOutputStream(writeFile);
        wb.write(out);


        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + xlsFileName);
        response.flushBuffer();
        OutputStream outputStream = response.getOutputStream();
        wb.write(response.getOutputStream());
        wb.close();
        outputStream.flush();
        outputStream.close();

        out.flush();
        out.close();
    }
}
